<?php
/**
 * QueryController.class.php
 * 
 * @author lsl8966
 * @copyright www.cnblog.com/lsl8966
 * @package Controller
 * @version $Id: GoodsController.class.php 1.0 2014-12-23 22:18:13Z $
 */
class ShopController extends Service{
   

    function __destruct(){
    }

  
    /**
     * 前函数
     */
    protected function init(){
         parent::init();        
     }

   /**
   **分类列表
   **/
   public function getkindListAction(){
	    $model_product = Controller::createMode('db_line6','t_bd_item_cls');
        $first_kind=$model_product->where("cls_parent='' and item_clsno < '03'")->select();
        foreach($first_kind as $key=>$val){
			$first_kind[$key]['item_clsname'] = iconv('GBK', 'UTF-8', $val['item_clsname']);
            //获取子分类信息
			$child_list = array();
            $child_list = $model_product->where("cls_parent='".$val['item_clsno']."'")->select();
			foreach($child_list as $k=>$cl){
				$child_list[$k]['item_clsname'] = iconv('GBK', 'UTF-8', $cl['item_clsname']);
			}
            //添加子分类列表
            $first_kind[$key]['child_list'] = is_array($child_list) ? $child_list : array();
            if(!$first_kind[$key]['child_list']){
                unset($first_kind[$key]);
            }
            foreach($first_kind[$key]['child_list'] as $thkey=>$throw ){
				$three_child_list = array();
                $three_child_list = $model_product->where("cls_parent='".$throw['item_clsno']."'")->select();
				foreach($three_child_list as $kk=>$cll){
					$three_child_list[$kk]['item_clsname'] = iconv('GBK', 'UTF-8', $cll['item_clsname']);
				}
                $first_kind[$key]['child_list'][$thkey]['child_list'] = is_array($three_child_list) ? $three_child_list : array();
            }
        }
		 $this->ajax(200,'ok',array('list'=>$first_kind));	   
   }
   
   public function getProductListAction(){
	    //TODO 分页
        $pagesize = $this->jsonObj->page_size?$this->jsonObj->page_size:20;
        $page = $this->jsonObj->p?$this->jsonObj->p:0;
        $pager = $this->instance('pager');
        $pager->loadCss('classic');
        if (!$page) {
            $start = 0;
        } else {
            $start = ($page - 1) * $pagesize;
        }
        $class_model = Controller::createMode('db_line6','t_bd_item_cls');
		$promtion_model = Controller::createMode('db_sys_26_71','t_26_product_promotion');
		$cart_model = Controller::createMode('db_sys_26_71','t_26_user_product_cart_list');
        $kind_id = $this->jsonObj->kind_id;
        $model_goods = Controller::createMode('db_line6','t_bd_item_info');
        $target = array();
        array_push($kind_ids,$kind_id);
        $where = " (item_clsno like '".$kind_id."%') and (item_clsno like '01%' or item_clsno like '02%') ";
		$data_total = $model_goods->execute("SELECT COUNT(1) as total_num FROM t_bd_item_info where ".$where);
        $data_total = $data_total[0]['total_num'];
		$top_size = $pagesize+$start;
		$goods_promotion = $promtion_model->execute("select item_no from t_26_product_promotion",true);
		$item_ids = array_column($goods_promotion,'item_no');
		if(count($item_ids)>0){
			//$where .= " and item_no not in ('".implode("','",$item_ids)."')";
		}
        $kind_goods = $model_goods->execute("select top $top_size * from t_bd_item_info where ".$where);
        $model_file = Controller::createMode('db_sys_26_71','t_26_attach');
		$kind_goods = array_splice($kind_goods,$start,$pagesize);
        foreach($kind_goods as $key=>$row){			
			$goods_stock = $model_goods->execute("select top 1 * from t_im_branch_stock where item_no = '".$row['item_no']."'");
			$kind_goods[$key]['item_name'] = iconv('GBK', 'UTF-8', $row['item_name']);
			$kind_goods[$key]['unit_no'] = iconv('GBK', 'UTF-8', $row['unit_no']);
			$kind_goods[$key]['stock_qty'] = round($goods_stock[0]['stock_qty'],2);
			$kind_goods[$key]['pro_code2'] = getProductRefundTitle($row);
			$cartinfo=$cart_model->getOne(array('m_id'=>$row['item_no'],'user_id'=>$this->jsonObj->user_id));
            $kind_goods[$key]['cart_total'] = floatval($cartinfo['total']);
			$pro_goods = $promtion_model->getOne("m_deliver_limit_time > '".date("Y-m-d H:i:s")."'  AND state=1 and item_no=".$row['item_no']);		
			//$pro_goods = $promtion_model->getOne(array('state'=>1,'item_no'=>$row['item_no']));						
			if( $pro_goods['price']>0){
				$kind_goods[$key]['scheme_price'] = $pro_goods['price'];
			}
        }
        $model_file->close();
        $params = array();
        if($this->get('kid')){
            $params['kid'] = $this->get('kid');
        }
      
        $pager_html = $pager->total($data_total)->num($pagesize)->page($page)->url($page_url)->output();

	   $this->ajax(200,'ok',array('total_num'=>$data_total,'list'=>$kind_goods));	   
   }
   
   /**
   *搜索商品
   **/
   public function searchTipsAction(){
        $jsonObj = $this->jsonObj;
        if($jsonObj->keyword){
			
            $sql = "select top 50 * from Line6.dbo.t_bd_item_info where  (item_clsno like '01%' or item_clsno like '02%') and  (item_rem  like '%".strtoupper(iconv("utf-8","gbk",$jsonObj->keyword))."%' or item_name  like '%".iconv("utf-8","gbk",$jsonObj->keyword)."%' or item_no like '%".iconv("utf-8","gbk",$jsonObj->keyword)."%') ";
            $promtion_model = Controller::createMode('db_sys_26_71','t_26_product_promotion');
			$goods_promotion = $promtion_model->execute("select item_no from t_26_product_promotion",true);
			$item_ids = array_column($goods_promotion,'item_no');
			if(count($item_ids)>0){
				//$sql .= " and item_no not in ('".implode("','",$item_ids)."')";
			}
			$model = Controller::createMode('db_line6','t_bd_item_info');
            $results = $model->execute($sql);			
            //名字与价格拼接成串
            foreach($results as $key=>$val){				
				$result[$key]['item_name'] = iconv("gbk","utf-8",$val['item_name']);
                $result[$key]['m_name'] = iconv("gbk","utf-8",$val['item_name']);
                $result[$key]['m_usual_price'] = '';
				$result[$key]['item_no'] = $val['item_no'];
				$result[$key]['m_id'] = $val['item_no'];
                $result[$key]['m_group_price'] = '';
            }

            self::$_extensionArray['o']['list'] = $result;
            //TODO 记录搜索历史,注释掉，避免a这种字母也写进我的搜索历史
            if(count($result)>0 && text::isChinese($this->jsonObj->keyword)){
                $modle_history = Controller::createMode('db_sys_26_71','t_26_sousuolishi');
                if($modle_history->getOne(array('keyword'=>$this->jsonObj->keyword,'user_id'=>$this->jsonObj->user_id))){
                    $modle_history->update(array('nums'=>'nums+1'),array('keyword'=>$this->jsonObj->keyword,'user_id'=>$this->jsonObj->user_id));
                }else{
                    $modle_history->insert(array('keyword'=>$this->jsonObj->keyword,'user_id'=>$this->jsonObj->user_id,'nums'=>1));
                }
            }
			$this->ajax(201,'ok',array('list'=>$result));
        }else{
            $this->ajax(201,null,array());
        }
    }
	
	public function getProductPromotionListAction(){
		$model_goods = Controller::createMode('db_sys_26_71','t_26_menu');
        $model_file = Controller::createMode('db_sys_26_71','t_26_attach');
        //t_26_sales_promotion
        $model_hots = Controller::createMode('db_line6','t_bd_item_info');
        $cart_model = Controller::createMode('db_sys_26_71','t_26_user_product_cart_list');
		
		$promotion_model = Controller::createMode('db_sys_26_71','t_26_product_promotion');
        $num=100;
        $hot_goods = $model_hots->execute("select top 20 * from t_bd_item_info where item_clsno like '01%' or item_clsno like '02%' order by build_date desc");
        foreach($hot_goods as $key=>$row){
			$goods_stock = $model_hots->execute("select top 1 * from t_im_branch_stock where item_no = '".$row['item_no']."'");
			$hot_goods[$key]['stock_qty'] = round($goods_stock[0]['stock_qty'],2);
			 $hot_goods[$key]['item_name'] = iconv('GBK', 'UTF-8', $row['item_name']);
			 $hot_goods[$key]['unit_no'] = iconv('GBK', 'UTF-8', $row['unit_no']);
			 $cartinfo=$cart_model->getOne(array('m_id'=>$row['item_no'],'user_id'=>$this->jsonObj->user_id));
				$hot_goods[$key]['cart_total'] = floatval($cartinfo['total']);
				$hot_goods[$key]['pro_code2'] = getProductRefundTitle($row);
		}
        $hot_tabs = array();
        if(is_array($sales_list)){
            $hot_tabs = array_merge($hot_tabs,$sales_list);
        }		
		$hot_ac =array(
            array(
                'title'=>'新品上架',
                'data' => array_slice($hot_goods,0,50)
            )
        );		
		$promotion_titles = $promotion_model->execute("select * from t_26_product_promotion_kind");		
		foreach($promotion_titles as $kind){			
			$promotion_goods = $promotion_model->where("state=1 and kind_id=".$kind['id'])->limit(50)->select();
			foreach($promotion_goods as $key=>$row){
				$goods = $model_hots->execute("select top 1 * from t_bd_item_info where item_no = '".$row['item_no']."'");
				$promotion_goods[$key] = array_merge($promotion_goods[$key],$goods[0]);
				 $promotion_goods[$key]['stock_qty'] = $row['m_stock'];
				 $promotion_goods[$key]['m_deliver_limit_time'] = $row['m_deliver_limit_time'];
				 $promotion_goods[$key]['item_size'] = $goods[0]['item_size'];
				 $promotion_goods[$key]['sale_price'] = $goods[0]['sale_price'];
				 $promotion_goods[$key]['current_price'] = $row['price'];
				 $promotion_goods[$key]['item_no'] = $goods[0]['item_no'];
				 $promotion_goods[$key]['item_name'] = iconv('GBK', 'UTF-8', $goods[0]['item_name']);
				 $promotion_goods[$key]['unit_no'] = iconv('GBK', 'UTF-8', $goods[0]['unit_no']);
				$cartinfo=$cart_model->getOne(array('m_id'=>$goods[0]['item_no'],'user_id'=>$this->jsonObj->user_id));
				$promotion_goods[$key]['cart_total'] = floatval($cartinfo['total']);
				$promotion_goods[$key]['pro_code2'] = getProductRefundTitle($goods[0]);
				 
			}
			$hot_ac[] = array(
                'title'=>$kind['title'],
                'data' => array_slice($promotion_goods,0,50),
            );
		}
        $hot_tabs = array_merge($hot_tabs,$hot_ac);
		$this->ajax(200,'ok',array('hot_tabs'=>$hot_tabs));
	}
	
	public function getPromotionListExAction(){
		$model_goods = Controller::createMode('db_sys_26_71','t_26_menu');
        $model_file = Controller::createMode('db_sys_26_71','t_26_attach');
        //t_26_sales_promotion
        $model_hots = Controller::createMode('db_line6','t_bd_item_info');
        $cart_model = Controller::createMode('db_sys_26_71','t_26_user_product_cart_list');
		
		$promotion_model = Controller::createMode('db_sys_26_71','t_26_product_promotion');
        $num=100;
        $hot_goods = $model_hots->execute("select top 20 * from t_bd_item_info where item_clsno like '01%' or item_clsno like '02%' order by build_date desc");
        foreach($hot_goods as $key=>$row){
			$goods_stock = $model_hots->execute("select top 1 * from t_im_branch_stock where item_no = '".$row['item_no']."'");
			$hot_goods[$key]['stock_qty'] = round($goods_stock[0]['stock_qty'],2);
			 $hot_goods[$key]['item_name'] = iconv('GBK', 'UTF-8', $row['item_name']);
			 $hot_goods[$key]['unit_no'] = iconv('GBK', 'UTF-8', $row['unit_no']);
			 $cartinfo=$cart_model->getOne(array('m_id'=>$row['item_no'],'user_id'=>$this->jsonObj->user_id));
				$hot_goods[$key]['cart_total'] = floatval($cartinfo['total']);
				$hot_goods[$key]['pro_code2'] = getProductRefundTitle($row);
		}
        $hot_tabs = array();
        if(is_array($sales_list)){
            $hot_tabs = array_merge($hot_tabs,$sales_list);
        }		
		$hot_ac =array(
            array(
                'title'=>'新品上架',
                'data' => array_slice($hot_goods,0,50),
            )
        );		
		$promotion_titles = $promotion_model->execute("select * from t_26_product_promotion_kind");		
		foreach($promotion_titles as $kind){			
			$promotion_goods = $promotion_model->where("state=1 and kind_id=".$kind['id'])->limit(50)->select();
			foreach($promotion_goods as $key=>$row){
				$goods = $model_hots->execute("select top 1 * from t_bd_item_info where item_no = '".$row['item_no']."'");
				$promotion_goods[$key] = array_merge($promotion_goods[$key],$goods[0]);
				 $promotion_goods[$key]['stock_qty'] = $row['m_stock'];
				 $promotion_goods[$key]['m_deliver_limit_time'] = $row['m_deliver_limit_time'];
				 $promotion_goods[$key]['item_size'] = $goods[0]['item_size'];
				 $promotion_goods[$key]['sale_price'] = $goods[0]['sale_price'];
				 $promotion_goods[$key]['current_price'] = $row['price'];
				 $promotion_goods[$key]['item_no'] = $goods[0]['item_no'];
				 $promotion_goods[$key]['item_name'] = iconv('GBK', 'UTF-8', $goods[0]['item_name']);
				 $promotion_goods[$key]['unit_no'] = iconv('GBK', 'UTF-8', $goods[0]['unit_no']);
				$cartinfo=$cart_model->getOne(array('m_id'=>$goods[0]['item_no'],'user_id'=>$this->jsonObj->user_id));
				$promotion_goods[$key]['cart_total'] = floatval($cartinfo['total']);
				$promotion_goods[$key]['pro_code2'] = getProductRefundTitle($goods[0]);
				 
			}
			$hot_ac[] = array(
                'title'=>$kind['title'],
                'data' => array_slice($promotion_goods,0,50),
            );
		}
        $hot_tabs = array_merge($hot_tabs,$hot_ac);
		$this->ajax(200,'ok',array('list'=>$hot_tabs));
	}
	
	
	
	
	  /***
     * 获取商品列表 （新增） 新品
     */
	public function getProductPromotionList_1Action(){
		$model_goods = Controller::createMode('db_sys_26_71','t_26_menu');
        $model_file = Controller::createMode('db_sys_26_71','t_26_attach');
        //t_26_sales_promotion
        $model_hots = Controller::createMode('db_line6','t_bd_item_info');
        $cart_model = Controller::createMode('db_sys_26_71','t_26_user_product_cart_list');
		
		$promotion_model = Controller::createMode('db_sys_26_71','t_26_product_promotion');
        $num=100;
        $hot_goods = $model_hots->execute("select top 20 * from t_bd_item_info where item_clsno like '01%' or item_clsno like '02%' order by build_date desc");
        foreach($hot_goods as $key=>$row){
			$goods_stock = $model_hots->execute("select top 1 * from t_im_branch_stock where item_no = '".$row['item_no']."'");
			$hot_goods[$key]['stock_qty'] = round($goods_stock[0]['stock_qty'],2);
			$hot_goods[$key]['item_name'] = iconv('GBK', 'UTF-8', $row['item_name']);
			$hot_goods[$key]['unit_no'] = iconv('GBK', 'UTF-8', $row['unit_no']);
			$cartinfo=$cart_model->getOne(array('m_id'=>$row['item_no'],'user_id'=>$this->jsonObj->user_id));
			$hot_goods[$key]['cart_total'] = floatval($cartinfo['total']);
			$hot_goods[$key]['pro_code2'] = getProductRefundTitle($row);
		}
        $hot_tabs = array();
        if(is_array($sales_list)){
            $hot_tabs = array_merge($hot_tabs,$sales_list);
        }		
		$hot_ac =array(
            array(
                'title'=>'新品上架',
                'data' => array_slice($hot_goods,0,50)
            )
        );		
		
        $hot_tabs = array_merge($hot_tabs,$hot_ac);
		$this->ajax(200,'ok',array('hot_tabs'=>$hot_tabs));
	}
	 
	  /***
     * 获取商品列表 （新增） 促销
     */
	public function getProductPromotionList_2Action(){
		$cart_model = Controller::createMode('db_sys_26_71','t_26_user_product_cart_list');
		$model_hots = Controller::createMode('db_line6','t_bd_item_info');
		$promotion_model = Controller::createMode('db_sys_26_71','t_26_product_promotion');
			$hot_tabs = array();
        if(is_array($sales_list)){
            $hot_tabs = array_merge($hot_tabs,$sales_list);
        }
		$hot_ac =array();		
		$promotion_titles = $promotion_model->execute("select * from t_26_product_promotion_kind");		
		foreach($promotion_titles as $kind){						
			$promotion_goods = $promotion_model->where("m_deliver_limit_time > '".date("Y-m-d H:i:s")."'  AND state=1 and kind_id=".$kind['id'])->limit(100)->select();
			foreach($promotion_goods as $key=>$row){
				$goods = $model_hots->execute("select top 1 * from t_bd_item_info where item_no = '".$row['item_no']."'");
				$promotion_goods[$key] = array_merge($promotion_goods[$key],$goods[0]);
				$promotion_goods[$key]['stock_qty'] = $row['m_stock'];
				$promotion_goods[$key]['m_deliver_limit_time'] = $row['m_deliver_limit_time'];
				$promotion_goods[$key]['item_size'] = $goods[0]['item_size'];
				//$promotion_goods[$key]['scheme_price'] = $goods_promotion['price'];
				$promotion_goods[$key]['sale_price'] = $goods[0]['sale_price'];
				$promotion_goods[$key]['min_price'] = $goods[0]['scheme_price'];
				$promotion_goods[$key]['scheme_price'] = $row['price'];
				$promotion_goods[$key]['item_no'] = $goods[0]['item_no'];
				$promotion_goods[$key]['item_name'] = iconv('GBK', 'UTF-8', $goods[0]['item_name']);
				$promotion_goods[$key]['unit_no'] = iconv('GBK', 'UTF-8', $goods[0]['unit_no']);
				$cartinfo=$cart_model->getOne(array('m_id'=>$goods[0]['item_no'],'user_id'=>$this->jsonObj->user_id));
				$promotion_goods[$key]['cart_total'] = floatval($cartinfo['total']);
				$promotion_goods[$key]['pro_code2'] = getProductRefundTitle($goods[0]);		 
			}
			$hot_ac[] = array(
                'title'=>$kind['title'],
                'data' => array_slice($promotion_goods,0,50),
            );
		}
        $hot_tabs = array_merge($hot_tabs,$hot_ac);
		$this->ajax(200,'ok',array('hot_tabs'=>$hot_tabs));
	}
	
	
	
	/**
	* 获取消息 (新增)
	*/
	public function messageAction(){
        $message_model = Controller::createMode('db_sys_26_71','t_26_notice_info');
        $list = $message_model->where('state = 1')->order("insert_dt desc")->select();
        $read_model = Controller::createMode('db_sys_26_71','t_26_notice_read');
        foreach($list as $key=>$val){
            $merchandise = $read_model->getOne(array('notice_id'=>$val['id'],'user_id'=> $this->jsonObj->user_id));
            if($merchandise){
                $list[$key]['read'] = 1;
            }else{
                $list[$key]['read'] = 0;
            }
        }
		$this->ajax(200,'ok',array('list'=>$list));
    }
	
	
	public function goodsDetailAction(){
		$model_goods = Controller::createMode('db_line6','t_bd_item_info');
		$class_model = Controller::createMode('db_sys_26_71','t_26_product_kind');
		$cart_model = Controller::createMode('db_sys_26_71','t_26_user_product_cart_list');  								
        if(!$this->get('kid')){           
            $kind = $class_model->where('parent_id=0')->order("order_id asc")->limit(1)->select();
            $kind2 = $class_model->where('parent_id='.$kind[0]['kind_id'])->order("order_id asc")->limit(1)->select();
            $kind3 = $class_model->where('parent_id='.$kind2[0]['kind_id'])->order("order_id asc")->limit(1)->select();
            $kind_id = $kind3[0]['kind_id'];
            $class_model->close();
        }else{
            $product_list= Controller::createMode('db_sys_26_71','t_26_order_product_list');
            $kind_id = $this->get('kid');
             $sql_history = "SELECT * FROM t_pm_sheet_detail where sheet_no like 'DY" . $this->jsonObj->user_id . "%' and item_no = '".$kind_id."'";
            $history_list= $model_goods->execute($sql_history);
			foreach($history_list as $key=>$val){	
				$work_date = (array)$val['produce_date'];
				$history_list[$key]['_date'] = $work_date['date'];
			}			
            $sql_sale='SELECT *,sum(total)  as sale_num ,left(FROM_UNIXTIME(time) ,10) as insert_dt FROM `t_26_order_product_list`  where  m_id =' .$kind_id. ' GROUP BY insert_dt limit 10';
            $sale_list=$product_list->execute($sql_sale);
        }       
		$user_cart_list = $cart_model->getOne(array('m_id'=>$kind_id,'user_id'=>$this->jsonObj->user_id));	

        $kind_goods = $model_goods->execute("select * from t_bd_item_info where item_no = '".$this->jsonObj->m_id."'");
		$promotion_data = $class_model->execute("select * from t_26_product_promotion where item_no = '".$kind_id."'",false);
		foreach($kind_goods as $key=>$val){				
				$kind_goods[$key]['item_name'] = iconv("gbk","utf-8",$val['item_name']);
				$goods_stock = $model_goods->execute("select top 1 * from t_im_branch_stock where item_no = '".$row['item_no']."'");
				$kind_goods[$key]['stock_qty'] = round($goods_stock[0]['stock_qty'],2);
				$kind_goods[$key]['unit_no'] = iconv('GBK', 'UTF-8', $val['unit_no']);
				$kind_goods[$key]['pro_code2'] = getProductRefundTitle($val);
		}

		
		$this->ajax(200,'ok',array('list'=>$kind_goods));
	}
	
	
	/**
	*  获取单个商品详情（新增修改）
	*/
		
	public function goodsDetail_1Action(){
		$model_goods = Controller::createMode('db_line6','t_bd_item_info');
		$class_model = Controller::createMode('db_sys_26_71','t_26_product_kind');
		$cart_model = Controller::createMode('db_sys_26_71','t_26_user_product_cart_list');  								
        if(!$this->get('kid')){           
            $kind = $class_model->where('parent_id=0')->order("order_id asc")->limit(1)->select();
            $kind2 = $class_model->where('parent_id='.$kind[0]['kind_id'])->order("order_id asc")->limit(1)->select();
            $kind3 = $class_model->where('parent_id='.$kind2[0]['kind_id'])->order("order_id asc")->limit(1)->select();
            $kind_id = $kind3[0]['kind_id'];
            $class_model->close();
        }else{
            $product_list= Controller::createMode('db_sys_26_71','t_26_order_product_list');
            $kind_id = $this->get('kid');
             $sql_history = "SELECT * FROM t_pm_sheet_detail where sheet_no like 'DY" . $this->jsonObj->user_id . "%' and item_no = '".$kind_id."'";
            $history_list= $model_goods->execute($sql_history);
			foreach($history_list as $key=>$val){	
				$work_date = (array)$val['produce_date'];
				$history_list[$key]['_date'] = $work_date['date'];
			}			
            $sql_sale='SELECT *,sum(total)  as sale_num ,left(FROM_UNIXTIME(time) ,10) as insert_dt FROM `t_26_order_product_list`  where  m_id =' .$kind_id. ' GROUP BY insert_dt limit 10';
            $sale_list=$product_list->execute($sql_sale);
        }    

        $cart_total = $cart_model->where('user_id='.$this->jsonObj->user_id)->count('t_26_user_product_cart_list');
	
		$user_cart_list = $cart_model->getOne(array('m_id'=>$kind_id,'user_id'=>$this->jsonObj->user_id));	

        $kind_goods = $model_goods->execute("select * from t_bd_item_info where item_no = '".$this->jsonObj->m_id."'");
		
		$promotion_data = $class_model->execute("select * from t_26_product_promotion where item_no = '".$kind_id."'",false);
		$promtion_model = Controller::createMode('db_sys_26_71','t_26_product_promotion');
		foreach($kind_goods as $key=>$val){	
				$is_cart = $cart_model->getOne(array('user_id'=>$this->jsonObj->user_id,'m_id'=>$val['item_no']));		
				$kind_goods[$key]['item_name'] = iconv("gbk","utf-8",$val['item_name']);
				$goods_stock = $model_goods->execute("select top 1 * from t_im_branch_stock where item_no = '".$val['item_no']."'");
				$kind_goods[$key]['stock_qty'] = round($goods_stock[0]['stock_qty'],2);
				$kind_goods[$key]['unit_no'] = iconv('GBK', 'UTF-8', $val['unit_no']);
				$kind_goods[$key]['pro_code2'] = getProductRefundTitle($val);		
				
				$goods = $promtion_model->getOne("m_deliver_limit_time > '".date("Y-m-d H:i:s")."' and state=1 and item_no=".$val['item_no']);
				
				if($goods && $goods['price'] > 0){
					$kind_goods[$key]['prom_price'] = $goods['price'] ;
				}
				$kind_goods[$key]['is_cart'] = 1;
				if($is_cart){
						$kind_goods[$key]['is_cart'] = 0;
				}
		}

		$this->ajax(200,'ok',array('list'=>$kind_goods,'cart_total'=>$cart_total));
	}
	
	 /***
     * 购物车商品列表
     */
    public function productcartListAction(){
        if( !$this->jsonObj->user_id){
            $this->ajax(201,null,null);
        }
		if(!$this->jsonObj->user_id) $this->ajax(201,'ok');
		$this->jsonObj->page_size=$this->jsonObj->page_size?$this->jsonObj->page_size:20;
		$cart_model = Controller::createMode('db_sys_26_71','t_26_user_product_cart_list');  
		$model_goods = Controller::createMode('db_line6','t_bd_item_info');
        $modelArray = array();

        $kind_goods = $cart_model->where("user_id='".$this->jsonObj->user_id."'")
		->limit(0, 200)
		->order('time desc')
		->select();
        if(!is_array($kind_goods)){
            $this->ajax(202,null,null);//没查到赞记录
        }
        if(count($kind_goods) == $this->jsonObj->page_size+1){
            self::$_extensionArray['o']['finish'] = 1;
            $kind_goods = array_slice($kind_goods,0,$this->jsonObj->page_size);
        }else{
            self::$_extensionArray['o']['finish'] = 0;
        }	
		foreach($kind_goods as $key=>$val){	
				$goods = $model_goods->execute("select * from t_bd_item_info where item_no = '".$val['m_id']."'");		
				$kind_goods[$key]['item_name'] = iconv("gbk","utf-8",$goods[0]['item_name']);
				$goods_stock = $model_goods->execute("select top 1 * from t_im_branch_stock where item_no = '".$val['m_id']."'");
				$kind_goods[$key]['stock_qty'] = round($goods_stock[0]['stock_qty'],2);
				$kind_goods[$key]['unit_no'] = iconv('GBK', 'UTF-8', $goods[0]['unit_no']);
				$kind_goods[$key]['item_size'] = $goods[0]['item_size'];
				$kind_goods[$key]['chasedo_spec'] = $goods[0]['chasedo_spec'];
				$kind_goods[$key]['pro_code2'] = getProductRefundTitle($goods[0]);
				//$goods = $cart_model->getOne("m_deliver_limit_time > '".date("Y-m-d H:i:s")."' and state=1 and item_no=".$vo['item_no']);
				$kind_goods[$key]['m_price'] = getProductSalePriceByUserType($this->jsonObj->user_type,$goods[0],1);
		}
        $this->ajax(200,null,array('list'=>$kind_goods));
    }
	
	/**
	* 添加至购物车
	**/
	public function addToCartAction(){
		$cart_model = Controller::createMode('db_sys_26_71','t_26_user_product_cart_list');
		$result = false;
		$current_dt = $cart_model->getOne(array('user_id'=>$this->jsonObj->user_id,'m_id'=>$this->jsonObj->m_id));
		$total_num=$this->jsonObj->total;
		if(!$this->jsonObj->user_id) $this->ajax(201,'ok');
		if(!$current_dt){			
			Log::write("[new_cart]\n".json_encode(
				array(
				'user_id'=>$this->jsonObj->user_id,
				'm_id'=>$this->jsonObj->m_id,
				'total'=>$this->jsonObj->total,
				'm_price'=>$this->jsonObj->price,
				'price'=>$this->jsonObj->price*$this->jsonObj->total,
				'time'=>time(),
				'create_time'=>date("Y-m-d H:i:s"),
				)
			)."\n", 'info', 'api.shop.cart');
			$result = $cart_model->insert(
				array(
				'user_id'=>$this->jsonObj->user_id,
				'm_id'=>$this->jsonObj->m_id,
				'total'=>$this->jsonObj->total,
				'm_price'=>$this->jsonObj->price,
				'price'=>$this->jsonObj->price*$this->jsonObj->total,
				'time'=>time(),
				'create_time'=>date("Y-m-d H:i:s"),
				)
			);			
		}else{
			Log::write("[add_cart]\nprev=".json_encode($current_dt)."\ncurrent=".json_encode(
				array(
				'm_price'=>$this->jsonObj->price,
				'price'=>$this->jsonObj->price*($current_dt['total']+$this->jsonObj->total),
				'total'=>$current_dt['total']+$this->jsonObj->total,
				'time'=>time()
				)
			)."\n", 'info', 'api.shop.cart');
			$result = $cart_model->update(
				array(
				'm_price'=>$this->jsonObj->price,
				'price'=>$this->jsonObj->price*($current_dt['total']+$this->jsonObj->total),
				'total'=>$current_dt['total']+$this->jsonObj->total,
				'time'=>time()
				),array(
				'user_id'=>$this->jsonObj->user_id,
				'm_id'=>$this->jsonObj->m_id
				)
			);	
			$total_num=	$current_dt['total']+$this->jsonObj->total;		
		}
		$this->ajax(200,'ok',array('total_num'=>$total_num));
	}
	
	/**
	* 添加至购物车 批量 （新增）
	**/
	public function addCartOrderAction(){
		 $order_model = Controller::createMode('db_sys_26_71','t_26_order_product_list');
		 $order_info = $order_model->where( array('orderid'=> $this->jsonObj->orderid))->select();
		 $cart_model = Controller::createMode('db_sys_26_71','t_26_user_product_cart_list');
		 if(!$this->jsonObj->user_id) $this->ajax(201,'ok');
		   foreach($order_info as $row){
				$cart_info = $cart_model->getOne( array('user_id'=> $this->jsonObj->user_id, 'm_id'=>$row['m_id']));
				if(empty($cart_info)){
					$res = $cart_model->insert(
					array(
					'user_id'=>$this->jsonObj->user_id,
					'm_id'=>$row['m_id'],
					'm_price'=>$row['price']/$row['total'],
					'total'=>$row['total'],
					'price'=>$row['price'], 
					'remark'=>$row['remark'], 
					'create_time'=>date("Y-m-d H:i:s"),
					'time'=>time()));
				} else {
					$total = $row['total'];
					$res = $cart_model->update(array(
						'time'=>time(),
						'm_price'=>$row['price']/$row['total'],
						'total'=>$row['total'],
						'price'=>$row['price'], 
						'remark'=>$row['remark'], 
					),array(
						'user_id'=> $this->jsonObj->user_id,
						'm_id'=>$row['m_id']
					));
				}				
		 }   
		 $this->ajax(200,'ok');
	}
	
	/**
	* 修改购物车
	**/
	public function updateToCartAction(){
		$cart_model = Controller::createMode('db_sys_26_71','t_26_user_product_cart_list');
		$result = false;
		$current_dt = $cart_model->getOne(array('user_id'=>$this->jsonObj->user_id,'m_id'=>$this->jsonObj->m_id));
		if(!$this->jsonObj->user_id) $this->ajax(201,'ok');
		if(!$current_dt){			
			$result = $cart_model->insert(
				array(
				'user_id'=>$this->jsonObj->user_id,
				'm_id'=>$this->jsonObj->m_id,
				'total'=>$this->jsonObj->total,
				'm_price'=>$this->jsonObj->price,
				'price'=>$this->jsonObj->price*$this->jsonObj->total,
				'create_time'=>date("Y-m-d H:i:s"),
				'time'=>time(),
				)
			);			
		}else{
			$cart_model->delete(array('user_id'=>$this->jsonObj->user_id,'m_id'=>$this->jsonObj->m_id));
			$result = $cart_model->update(
				array(
				'm_price'=>$this->jsonObj->price,
				'price'=>$this->jsonObj->price*($current_dt['total']+$this->jsonObj->total),
				'total'=>$current_dt['total']+$this->jsonObj->total,
				'time'=>time()
				),array(
				'user_id'=>$this->jsonObj->user_id,
				'm_id'=>$this->jsonObj->m_id
				)
			);				
		}
		$this->ajax(200,'ok');
	}	
	
	
	
	/**
	* 修改购物车 (修改)
	**/
	public function updateToCart1Action(){
		$cart_model = Controller::createMode('db_sys_26_71','t_26_user_product_cart_list');
		$result = false;
		if(!$this->jsonObj->user_id) $this->ajax(201,'ok');
		$current_dt = $cart_model->getOne(array('user_id'=>$this->jsonObj->user_id,'m_id'=>$this->jsonObj->m_id));
		if(!$current_dt){		
			Log::write("[update_cart].\n".json_encode(
				array(
				'user_id'=>$this->jsonObj->user_id,
				'm_id'=>$this->jsonObj->m_id,
				'total'=>$this->jsonObj->total,
				'm_price'=>$this->jsonObj->price,
				'price'=>$this->jsonObj->price*$this->jsonObj->total,
				'create_time'=>date("Y-m-d H:i:s"),
				'time'=>time(),
				)
			)."\n", 'info', 'api.shop.cart');	
			$result = $cart_model->insert(
				array(
				'user_id'=>$this->jsonObj->user_id,
				'm_id'=>$this->jsonObj->m_id,
				'total'=>$this->jsonObj->total,
				'm_price'=>$this->jsonObj->price,
				'price'=>$this->jsonObj->price*$this->jsonObj->total,
				'time'=>time(),
				'create_time'=>date("Y-m-d H:i:s"),
				)
			);			
		}else{
			Log::write("[update_cart].\nprev=".json_encode($current_dt)."\ncurrent=".json_encode(
				array(
					'm_price'=>$this->jsonObj->price,
					'price'=>$this->jsonObj->price*($this->jsonObj->total),
					'total'=>$this->jsonObj->total,
					'time'=>time()
				)
			)."\n", 'info', 'api.shop.cart');
			$result = $cart_model->update(
				array(
				'm_price'=>$this->jsonObj->price,
				'price'=>$this->jsonObj->price*($this->jsonObj->total),
				'total'=>$this->jsonObj->total,
				'time'=>time()
				),array(
				'user_id'=>$this->jsonObj->user_id,
				'm_id'=>$this->jsonObj->m_id
				)
			);				
		}
		$this->ajax(200,'ok');
	}
	
	/**
     * select
     */
    public function orders1Action(){
		$order_model = Controller::createMode('db_sys_26_71','t_26_order_info');
        $page_size = $this->jsonObj->page_size?$this->jsonObj->page_size:20;     
        $where = $this->parseWhere();
        $order = $this->parseOrder();
        if(count($order)>0){
            $list = $order_model->where($where)->field($fileds)->limit($page_size+1)->order($order)->select();
        }else {
            $list = $order_model->where($where)->field($fileds)->limit($page_size+1)->select();
        }
        if(count($list) == $page_size+1){
            self::$_extensionArray['o']['finish'] = 1;
            self::$_extensionArray['o']['list'] = array_slice($list,0,$page_size);
        } else {
            self::$_extensionArray['o']['finish'] = 0;
            self::$_extensionArray['o']['list'] = is_array($list)?$list:array();
        }
        self::$_extensionArray['o']['list'] = is_array(self::$_extensionArray['o']['list'])?self::$_extensionArray['o']['list']:array();
		foreach(self::$_extensionArray['o']['list'] as $key => $row){
			self::$_extensionArray['o']['list'][$key]['info'] = $order_model->execute("select * from t_26_order_product_list where orderid = '".$row['orderid']."'");
			
			
		}
        $this->ajax(200,null,self::$_extensionArray['o']);
    }
	
	public function ordersAction(){
        //TODO 分页
        $pagesize = 10;       
        $where = "user_id = ".$this->jsonObj->user_id;
        $order = "insert_dt desc";
        if($this->jsonObj->ordtime_start){
            $ordtime = $this->jsonObj->ordtime_start;
            $where .= " and insert_dt > '".$ordtime."'";
        }
        if($this->jsonObj->ordtime_end){
            $ordtime_end = $this->jsonObj->ordtime_end;
            $where .= " and insert_dt < '".$ordtime_end."'";
        }
        
        $orders_model = Controller::createMode('db_line6','t_pm_sheet_master');
		$where = "branch_no =  ".$this->jsonObj->user_id;
		$data_total = $orders_model->execute("SELECT COUNT(1) as total_num FROM t_pm_sheet_master where ".$where);
        $data_total = $data_total[0]['total_num'];
		$top_size = $pagesize+$start;
		
        $list = $orders_model->execute("select top $top_size * from t_pm_sheet_master where ".$where." order by  work_date desc");
		$list = array_slice($list,$start,$pagesize);
		foreach($list as $key=>$order ){
			$list[$key]['info'] = $orders_model->execute("select top 1 * from t_pm_sheet_detail where sheet_no ='".$order['sheet_no']."' order by  1");
			$dt = (array)$order['work_date'];
			$list[$key]['order_dt'] = $dt['date'];
			$list[$key]['memo'] = iconv('GBK', 'UTF-8', $order['memo']);
			foreach($list[$key]['info'] as $okey=>$oitem){
				$goods = $orders_model->execute("select * from t_bd_item_info where item_no = '".$oitem['item_no']."'");		
				$list[$key]['info'][$okey]['item_name'] = iconv("gbk","utf-8",$goods[0]['item_name']);
				$list[$key]['info'][$okey]['unit_no'] = iconv('GBK', 'UTF-8', $goods[0]['unit_no']);
				$list[$key]['info'][$okey]['item_size'] = $goods[0]['item_size'];
				$list[$key]['info'][$okey]['chasedo_spec'] = $goods[0]['chasedo_spec'];	
				$list[$key]['info'][$okey]['pro_code2'] = getProductRefundTitle($goods[0]);
			}
			
		}
        $this->ajax(200,null,array('list'=>$list));
    }
	
	
	/**
	* 获取订单数据修改 去除订单详情 （新增）
	**/
	public function orderkAction(){
        //TODO 分页
        $pagesize = 10;       
        $where = "user_id = ".$this->jsonObj->user_id;
        $order = "insert_dt desc";
        if($this->jsonObj->ordtime_start){
            $ordtime = $this->jsonObj->ordtime_start;
            $where .= " and insert_dt > '".$ordtime."'";
        }
        if($this->jsonObj->ordtime_end){
            $ordtime_end = $this->jsonObj->ordtime_end;
            $where .= " and insert_dt < '".$ordtime_end."'";
        }
        
        $orders_model = Controller::createMode('db_line6','t_pm_sheet_master');
		$where = "branch_no =  ".$this->jsonObj->user_id;
		$data_total = $orders_model->execute("SELECT COUNT(1) as total_num FROM t_pm_sheet_master where ".$where);
        $data_total = $data_total[0]['total_num'];
		$top_size = $pagesize+$start;
		
        $list = $orders_model->execute("select top $top_size * from t_pm_sheet_master where ".$where." order by  work_date desc");
		$list = array_slice($list,$start,$pagesize);
		foreach($list as $key=>$order ){
			$dt = (array)$order['work_date'];
			$list[$key]['order_dt'] = $dt['date'];
			$list[$key]['memo'] = iconv('GBK', 'UTF-8', $order['memo']);
		}
        $this->ajax(200,null,array('list'=>$list));
    }
	
	/**
     * 创建订单
     */
    public function createOrderAction(){
        $jPack = $_POST['json']?$_POST['json']:$_GET['json'];
		if($jPack){
             $jPack = urldecode($jPack);
             $jPack = stripcslashes($jPack);//过滤特殊字符
        }
		
        $jPack = str_replace("\\", '\\\\', $jPack);
		$list = (json_decode($jPack,true));
		
		Log::write("[create_order]\nrequest=".json_encode($list['info'])."\ndatabase=".json_encode(
			Controller::createMode('db_sys_26_71', 't_26_user_product_cart_list')->where(array('user_id'=>$list['user_id']))->select()
		)."\n", 'info', 'api.shop.cart.create_order');
		
		$user_id = $list['user_id'];
		$order_model = Controller::createMode('db_line6','t_pm_sheet_master');
		$order1_model = Controller::createMode('db_sys_26_71','t_26_order_info');
		$order2_mode2 = Controller::createMode('db_sys_26_71','t_26_order_product_list');
		//echo "select top 1 * from t_pm_sheet_master where branch_no = '".cookie :: get('account_branch_no')."' order by sheet_no desc ";
		
		$last_order  = $order_model->execute("select top 1 sheet_no from t_pm_sheet_master where branch_no = '".$user_id."' order by sheet_no desc ");
		$last_sheet_no = ($last_order[0]['sheet_no']);
		$index = substr($last_sheet_no,2+strlen($user_id)+6);
		$this_index = str_pad((int)$index+1,4,'0',STR_PAD_LEFT);
		$this_order_no = 'DY'.$user_id.date("ymd").$this_index;
		$model_change = Controller::createMode('db_sys_26_71','t_26_order_state_change');
		$total_amt=0;
		foreach($list['info'] as $row){
			$merchandises = $order_model->execute("select top 1 * from t_bd_item_info where item_no = '".$row['m_id']."'");
			$total_amt += $row['total']*$row['m_price'];
		}
		$total_amt = round($total_amt,1);
		//判断每个用户每天限制下单		
		if($list){
			$limit_orders = $order1_model->execute("select count(1) as total_num from t_26_order_info where user_id = ".$user_id." and insert_dt like '".date("Y-m-d")."%'",false);
			if($limit_orders['total_num']>1 && $user_id!='0161'){
				echo json_encode(array('code'=>202,'msg'=>'每天限制1个订单,您已经有'.$limit_orders['total_num'].'个订单了'));exit;//exit;
			}
			$current_time = date("Y-m-d H:i:s");
			$order1_model->insert(array(
				'price'=>$total_amt,
				'orderid'=>$this_order_no,
				'user_id'=>$user_id,
				'insert_dt'=>$current_time,
				'client_type'=>2,
				'truename'=>'',
				'address'=>'',
				'tel'=>'',
				'deliver_time'=>$list['deliver_time'],
				'buytime'=>time(),
				'remark'=>$list['remark']
			));			
			$sql = "insert into t_pm_sheet_master (sheet_no,sheet_amt,order_amt,trans_no,branch_no,d_branch_no,db_no,branch_flag,approve_flag,oper_date,work_date,valid_date,order_status,memo,oper_id,confirm_man,order_man) values (
			'".$this_order_no."',
			".$total_amt.",
			".$total_amt.",
			'DY',
			'".$user_id."',
			'0000',
			'+',
			'".$user_id."',
			1,
			'".date("Y-m-d H:i:s")."',
			'".date("Y-m-d H:i:s")."',
			'".date('Y-m-d H:i:s', strtotime('+30days'))."',
			0,
			'".iconv('UTF-8', 'GBK', $list['remark'])."',
			'".$user_id."',
			'".$user_id."',
			'".$user_id."'
			)";		
			$model_change->insert(
				array(
				'orderid'=>$this_order_no,
				'title'=>'您的订单提交成功',
				'create_time'=>date("Y-m-d H:i:s"),
				'sate'=>1
				)
			);			
			//写入订单主表
			if($order_model->execute($sql)){
				//写入订单明细表
				foreach($list['info'] as $row){
					$row['price'] = $row['m_price'];
					$merchandises = $order_model->execute("select top 1 * from t_bd_item_info where item_no = '".$row['m_id']."'");
					//$row['price'] = ($row['price']/($row['total']/$merchandises[0]['chasedo_spec']));					
					 $sql = "insert into t_pm_sheet_detail (sheet_no,item_no,memo,sub_amt,sale_price,valid_price,real_qty,produce_date) values (
						'".$this_order_no."',
						'".$row['m_id']."',
						'".iconv('UTF-8', 'GBK', $row['remark'])."',						
						".($row['total']*$row['price']).",
						".$row['price'].",
						".$row['price'].",
						".$row['total'].",
						'".date("Y-m-d H:i:s")."'
					)";
					$order_model->execute($sql);
					$order2_mode2->insert(array(
						'price'=>($row['total']*$row['price']),
						'total'=>$row['total'],
						'm_id'=>$row['m_id'],
						'orderid'=>$this_order_no,
						'user_id'=>$user_id,
						'time'=>time(),
						'remark'=>$row['remark']
					));
				}
			}
			echo json_encode(array('code'=>200,'msg'=>'订单提交成功','orderid'=>$this_order_no));
		}else{
			echo json_encode(array('code'=>201,'msg'=>'订单提交失败'));
		}
		
		exit;
    }
	
	
	/**
     * 我的收藏 (修改) 
     */
    public function collectsAction(){
        $collec_model = Controller::createMode('db_sys_26_71','t_26_70_collection');
        $m_model = Controller::createMode('db_line6','t_bd_item_info');
        $list = $collec_model->where(array('user_id'=>$this->jsonObj->user_id))->select();
		$cart_model = Controller::createMode('db_sys_26_71','t_26_user_product_cart_list');
        foreach($list as $key=>$val){
            $merchandises = $m_model->execute("select * from t_bd_item_info where item_no = '".$val['c_id']."'");
			$merchandise = $merchandises[0];
            if($merchandise){
                $list[$key]['m_name'] =  iconv('GBK', 'UTF-8', $merchandise['item_name']);
                $list[$key]['m_price'] = getProductSalePriceByUserType($this->jsonObj->user_type,$merchandise);
				$list[$key]['sale_price'] = round($merchandise['sale_price'],2);
				$list[$key]['tag_name'] = $merchandise['unit_no'].$merchandise['item_size'];
                $list[$key]['m_group_price'] = $merchandise['m_group_price'];
				$list[$key]['chasedo_spec'] = $merchandise['chasedo_spec'];
				$cartinfo = $cart_model->getOne(array('m_id'=>$val['c_id'],'user_id'=>$this->jsonObj->user_id));
				$list[$key]['cart_total'] = floatval($cartinfo['total']);
				$list[$key]['item_size'] = $merchandise['item_size'];
				$list[$key]['unit_no'] = iconv('GBK', 'UTF-8', $merchandise['unit_no']);
            }
        }
		$this->ajax(200,null,array('list'=>$list));
    }
	
	/**
     * 我的收藏添加至购物车 批量 (新增) 
     */
	
	public function AddAllToCartAction(){
		$collection_model = Controller::createMode('db_sys_26_71','t_26_70_collection');
        $m_model = Controller::createMode('db_line6','t_bd_item_info');
        $list = $collection_model->where(array('user_id'=>$this->jsonObj->user_id))->select();
		$cart_model = Controller::createMode('db_sys_26_71','t_26_user_product_cart_list');
		$res = false;
        foreach($list as $key=>$val){
            $merchandises = $m_model->execute("select * from t_bd_item_info where item_no = '".$val['c_id']."'");
			$cart_info = $cart_model->getOne( array('user_id'=> $this->jsonObj->user_id, 'm_id'=>$val['c_id']));
				if(empty($cart_info)){
					$res = $cart_model->insert(
					array(
					'user_id'=>$this->jsonObj->user_id,
					'm_id'=>$val['c_id'],
					'm_price'=>$merchandises[0]['scheme_price'],
					'total'=>$merchandises[0]['chasedo_spec'],
					'price'=>$merchandises[0]['chasedo_spec']*$merchandises[0]['scheme_price'], 
					'remark'=>'', 
					'time'=>time()));
				} else {
					$res = $cart_model->update(array(
						'time'=>time(),
						'm_price'=>$merchandises[0]['scheme_price'],
						'total'=>$cart_info[total]+$merchandises[0]['chasedo_spec'],
						'price'=>($cart_info[total]+$merchandises[0]['chasedo_spec'])*$merchandises[0]['scheme_price'], 
						'remark'=>'', 
					),array(
						'user_id'=> $this->jsonObj->user_id,
						'm_id'=>$val['c_id']
					));
				}	 
        }
		if($res){
				$this->ajax(200,'ok');
		}else{
				$this->ajax(201,'fail');
		}
	}
	
	/**
     * 我的收藏
     */
    public function collectAction(){
        $cart_model = Controller::createMode('db_sys_26_71','t_26_70_collection');
        $m_model = Controller::createMode('db_line6','t_bd_item_info');
        $list = $cart_model->where(array('user_id'=>$this->jsonObj->user_id))->select();
        foreach($list as $key=>$val){
            $merchandises = $m_model->execute("select * from t_bd_item_info where item_no = '".$val['c_id']."'");
			$merchandise = $merchandises[0];
            if($merchandise){
                $list[$key]['m_name'] =  iconv('GBK', 'UTF-8', $merchandise['item_name']);
                $list[$key]['m_price'] = getProductSalePriceByUserType($this->jsonObj->user_type,$merchandise);
				$list[$key]['sale_price'] = round($merchandise['sale_price'],2);
				$list[$key]['tag_name'] = $merchandise['unit_no'].$merchandise['item_size'];
                $list[$key]['m_group_price'] = $merchandise['m_group_price'];
				$list[$key]['chasedo_spec'] = $merchandise['chasedo_spec'];
				$list[$key]['item_size'] = $merchandise['item_size'];
				$list[$key]['unit_no'] = iconv('GBK', 'UTF-8', $merchandise['unit_no']);
				$list[$key]['pro_code2'] = getProductRefundTitle($merchandise);
                $spec_name = getProductSpecName($merchandise['m_spec']);
                if($merchandise['tag_name']){
                    $spec_name = $merchandise['tag_name'].'/'.$spec_name;
                }
                $list[$key]['tag_name'] = $spec_name;
            }
        }
		$this->ajax(200,null,array('list'=>$list));
    }
	
	
	/**
     * 添加至我的收藏 （新增）
     */
    public function addCollectAction(){
  
            $cart_model = Controller::createMode('db_sys_26_71','t_26_70_collection');
            $cart_info = $cart_model->getOne( array('user_id'=> $this->jsonObj->user_id, 'c_id'=>$this->jsonObj->item_no));
			$res = false;
            if(empty($cart_info)){
                $res = $cart_model->insert(array('user_id'=>$this->jsonObj->user_id,'c_id'=>$this->jsonObj->item_no,'nums'=>$this->jsonObj->nums, 'update_time'=>time()));
            } else {
                $res = $cart_model->update(array(
                    'update_time'=>time(),
                    'nums'=>$cart_info['nums']+$this->jsonObj->nums,
                ),array(
                    'user_id'=> $this->jsonObj->user_id,
                    'c_id'=>$this->jsonObj->item_no
                ));
            }
            if($res){
				$this->ajax(200,'ok');
            }else{
				$this->ajax(201,'fail');
            }
	}
	 /**
     * 删除我的收藏 (新增)
     */
    public function delCollectAction(){
            $cart_model = Controller::createMode('db_sys_26_71','t_26_70_collection');
            $map['user_id'] =$this->jsonObj->user_id;
            $map['c_id'] = $this->jsonObj->item_no;
            if($cart_model->delete($map)){
              $this->ajax(200,'ok');
            }else{
             $this->ajax(201,'fail');
            }
    }
	
	public function updatePwdAction(){
		$model_user  = Controller::createMode('db_sys_26_71','t_26_yonghuliebiao');
		$user = $model_user->getOne(array("mobile"=>$this->jsonObj->account));
		
		if(!$user){
			$response_obj->code = 202;//账号不存在，请联系管理员
			$response_obj->msg = '账号不存在，请联系管理员';
		}else{
			if($this->jsonObj->old_password != $user['password']){
				$response_obj->code = 203;//原始密码有误
				$response_obj->msg = '原始密码有误';
			}else{				
				if($model_user->update(array('password'=>$this->jsonObj->new_password),array("mobile"=>$this->jsonObj->account))){
					$response_obj->code = 200;//原始密码有误
					$response_obj->msg = '密码修改成功';
				}else{
					$response_obj->code = 204;//原始密码有误
					$response_obj->msg = '密码修改失败，请重试';
				}
			}		 
		}
		$this->ajax($response_obj->code,$response_obj->msg,null);		
	}
	
	/**
     * 登录
     */
    public function loginAction(){	
		$model_user  = Controller::createMode('db_sys_26_71','t_26_yonghuliebiao');
		$model  = Controller::createMode('db_line6','t_sys_operator');
		$result = $model->execute("select top 1 * from t_sys_operator where oper_id='".$this->jsonObj->account."' or oper_name='".$this->jsonObj->account."'");
		$user = $model_user->getOne(array("mobile"=>$this->jsonObj->account));
		if(!$result){
			$response_obj->code = 202;
			$response_obj->msg = '账号不存在，请联系管理员';
		} else {
			if(!$user &&  ($this->jsonObj->password) != (md5("666666"))){
				$response_obj->code = 201;
				$response_obj->msg = '初始密码错误，请联系管理员';
			}else if(($this->jsonObj->password) != $user['password']){
				$response_obj->code = 203;
				$response_obj->msg = '密码错误';
			}else if($user['state']!=1){
				$response_obj->code = 205;
				$response_obj->msg = '账号被冻结，请联系管理员';
			}else{
				$response_obj->code = 200;
			}
			
			if(!$user){
				if($result[0]){
						$da = array(
						'mobile'=>$this->jsonObj->account,
						'user_name'=>iconv('GBK', 'UTF-8',  $result[0]['oper_name']),
						'password'=> md5("666666")						
					);						
					$model_user->insert($da);
				}				
			}
		}	
		if($response_obj->code == 200){
			$branch_info = $model->execute("select top 1 * from t_bd_branch_info where branch_no = '".$result[0]['oper_id']."' ");	
			$response_obj->user_id = $this->jsonObj->account;
			$response_obj->user_type = $branch_info[0]['price_option'];			
			$response_obj->user_name = iconv('GBK', 'UTF-8',  $branch_info[0]['branch_name']);			
		}	
		echo json_encode($response_obj);exit;
        if($_POST){		
			$model_user  = Controller::createMode('db_sys_26_71','t_26_yonghuliebiao');
			$model  = Controller::createMode('db_line6','t_sys_operator');
			$result = $model->execute("select top 1 * from t_sys_operator where oper_id='".$this->jsonObj->account."' or oper_name='".$this->jsonObj->account."'");
			$user = $model_user->getOne(array("mobile"=>$this->jsonObj->account));
			if(!$result){
				$response_obj->code = 202;
			} else {
				if(!$user &&  md5($this->jsonObj->password) != (md5("666666"))){
					$response_obj->code = 201;
				}else if(md5($this->jsonObj->password) != $user['password']){
					$response_obj->code = 203;
				}else if($user['state']!=1){
					$response_obj->code = 205;
				}else{
					$response_obj->code = 200;
				}
				
				if(!$user){
					if($result[0]){
							$da = array(
							'mobile'=>$this->jsonObj->account,
							'user_name'=>iconv('GBK', 'UTF-8',  $result[0]['oper_name']),
							'password'=> md5("666666")						
						);						
						$model_user->insert($da);
					}				
				}
			}
			
			$response_obj->code;
			 //set cookie
			 if($response_obj->code == 200){
					$branch_info = $model->execute("select top 1 * from t_bd_branch_info where branch_no = '".$result[0]['oper_id']."' ");				
					session :: set(self::getUserIdKey(), $result[0]['oper_id']);
					cookie :: set('account_oper_address', iconv('GBK', 'UTF-8', $branch_info[0]['address']), 28800);
					cookie :: set('account_oper_branch_tel',$branch_info[0]['branch_tel'], 28800);
					session :: set(self::getUserNameKey(), $result[0]['oper_name']);
					session :: set(self::getUserMobileKey(), $result[0]['branch_no']);
					// cookie设置的周期为8小时
					cookie :: set('pcorder_login_status', true, 28800);
					cookie :: set(self::getUserIdKey(), $result[0]['branch_no'], 28800);
					cookie :: get(self::getUserIdKey());
					cookie :: set(self::getUserNameKey(), $result[0]['oper_name'], 28800);
					cookie :: set('account_oper_id', $result[0]['oper_id'], 28800);	
					cookie :: set('account_branch_price_option', $branch_info[0]['price_option'], 28800);	
					cookie :: set('account_branch_no', $result[0]['branch_no'], 28800);	
					cookie :: set('account_user_name', iconv('GBK', 'UTF-8',  $result[0]['oper_name']), 28800);					
					cookie :: set('account_branch_name', iconv('GBK', 'UTF-8',  $branch_info[0]['branch_name']), 28800);						
					//set session
					//$this->showMessage("登录成功，正在跳转",$this->_createUrl('index/index'),2);
			}elseif($response_obj->code == 201){
					//$this->showMessage("您还没有设置密码，请使用默认密码66666登录，登陆后尽快设置密码，请重试{$response_obj->code}",-1,5);
			}elseif($response_obj->code == 203){
					//$this->showMessage("密码错误，请重试",-1,2);
			}elseif($response_obj->code == 202){
					//$this->showMessage("账号不存在，请联系管理员",-1,2);
			}else{
					//$this->showMessage("登录失败，请联系管理员",-1,2);
			}
			exit;
        }
    }
	
	 public function orderdetailAction(){
        if($this->jsonObj->orderid){
			$orderid = $this->jsonObj->orderid;
			$model_this_order = Controller::createMode('db_sys_26_71','t_26_order_info');
			$model_this_order_product = Controller::createMode('db_sys_26_71','t_26_order_product_list');
			
			$my_order = $model_this_order->getOne(array('orderid'=>$orderid));
			$my_order_product_list = $model_this_order_product->where(array('orderid'=>$orderid))->select();
			
            $model_order = Controller::createMode('db_line6','t_pm_sheet_master');
            $model_menu = Controller::createMode('db_line6','t_bd_item_info');
			
            $order_infos = $model_order->execute("select top 1 * from t_pm_sheet_master where sheet_no='".$orderid."'");
			
			 
			//var_dump($order);
			
			
            $model = Controller::createMode('db_line6','t_pm_sheet_detail');		  		
            $data = $model->execute("select top 1000 * from t_pm_sheet_detail where sheet_no='".trim($order_infos[0]['sheet_no'])."'");
			
            $model_change = Controller::createMode('db_sys_26_71','t_26_order_state_change');            
			//拣货
			$jianhuos = $model_order->execute("select top 1 * from t_rf_sheet_master where voucher_no='".$this->jsonObj->orderid."'");
			if($jianhuos){
				$dt = (array)$jianhuos[0]['oper_date'];
				if(!$model_change->getOne(array('state'=>3,'orderid'=>$this->jsonObj->orderid))){
					$model_change->insert(
						array(
						'title'=>'您的订单正在分拣配货',
						'create_time'=>$dt['date'],
						'state'=>3,
						'orderid'=>$this->jsonObj->orderid
						)
					);
				}				
			}
			//开始配送
			$diaochu = $model_order->execute("select top 1 * from t_im_sheet_master where voucher_no='".$this->jsonObj->orderid."'");
			if($diaochu){
				$dt = (array)$diaochu[0]['oper_date'];
				if(!$model_change->getOne(array('state'=>4,'orderid'=>$this->jsonObj->orderid))){
					$model_change->insert(
						array(
						'title'=>'您的订单已经配货,复核完成',
						'create_time'=>$dt['date'],
						'state'=>4,
						'orderid'=>$this->jsonObj->orderid
						)
					);
				}				
			}
			//开始配送
			$diaochu = $model_order->execute("select top 1 * from t_im_sheet_master where voucher_no='".$this->jsonObj->orderid."'");
			//var_dump($diaochu);
			if($diaochu && trim($diaochu[0]['delivery_id']) != '' && trim($diaochu[0]['driver_id']) != ''){
				$dt = (array)$diaochu[0]['oper_date'];
				$driver = $model_order->execute("select top 1 * from t_sys_operator where oper_id='".$diaochu[0]['driver_id']."'");
				
				$delivery = $model_order->execute("select top 1 * from t_sys_operator where oper_id='".$diaochu[0]['delivery_id']."'");
				if(!$model_change->getOne(array('state'=>5,'orderid'=>$this->jsonObj->orderid))){
					$model_change->insert(
						array(
						'title'=>"您的订单已经分配给司机:".iconv('gb2312','utf-8',$driver[0]['oper_name']).",送货员:".iconv('gb2312','utf-8',$delivery[0]['oper_name'])."，正在火速赶往您店",
						'create_time'=>$dt['date'],
						'state'=>5,
						'orderid'=>$this->jsonObj->orderid
						)
					);
				}				
			}
			$change = $model_change->where(array('orderid'=>$this->jsonObj->orderid))->order("state desc,create_time DESC")->select();
            $ord_nums = 0;
			
            foreach($my_order_product_list as $key=>$product){						
				//$row['item_no'] = $product['m_id'];
				$product_list_item = $model_this_order_product->getOne(array('m_id'=>$product['item_no'],'orderid'=>$this->jsonObj->orderid));				
				$merchandises = $model_order->execute("select top 1 * from t_bd_item_info where item_no = '".$product['m_id']."'");
				$ord_nums++;						
				$my_order_product_list[$key]['item_no'] = 	$product['m_id'];
				$my_order_product_list[$key]['m_price'] = 	$product['price']/$product['total'];				
                $my_order_product_list[$key]['m_name'] = iconv('gb2312','utf-8',$merchandises[0]['item_name']);
                $my_order_product_list[$key]['m_min_price'] = $product_list_item['m_min_price'];
				$my_order_product_list[$key]['unit_no'] = iconv('gb2312','utf-8',$merchandises[0]['unit_no']);
				$my_order_product_list[$key]['item_size'] = $merchandises[0]['item_size'];
				$my_order_product_list[$key]['memo'] = iconv('gb2312','utf-8',$data[$key]['memo']);
                $my_order_product_list[$key]['m_group_price'] = $product_list_item['m_group_price'];
                $my_order_product_list[$key]['bar_code'] = $product_list_item['bar_code'];
            }
			//var_dump($my_order_product_list);exit;
			$this->ajax(200,null,array('list'=>$my_order_product_list,'changes'=>$change,'orderinfo'=>$my_order));			
        }
    }
	
	public function searchshopAction(){
			$keywords = $this->jsonObj->keywords;
            $pagesize = 20;
            $start = 0;
			$item_pinyin = strtoupper($keywords);
			if($item_pinyin){
				$sql = "select top 50 * from Line6.dbo.t_bd_item_info where (item_rem  like '%".strtoupper(iconv("utf-8","gbk",$keywords))."%' or item_name  like '%".iconv("utf-8","gbk",$keywords)."%' or item_no  like '%".iconv("utf-8","gbk",$keywords)."%')  and (item_clsno like '01%' or item_clsno like '02%')";
			}else{
				$sql = "select top 50 * from Line6.dbo.t_bd_item_info where  (item_name  like '%".iconv("utf-8","gbk",$keywords)."%' or item_no  like '%".iconv("utf-8","gbk",$keywords)."%') and (item_clsno like '01%' or item_clsno like '02%')";
			}
            $model = Controller::createMode('db_line6','t_bd_item_info');
			$promtion_model = Controller::createMode('db_sys_26_71','t_26_product_promotion');
			$goods_promotion = $promtion_model->execute("select item_no from t_26_product_promotion",true);
			$item_ids = array_column($goods_promotion,'item_no');
			if(count($item_ids)>0){
				//$sql .= " and item_no not in ('".implode("','",$item_ids)."')";
			}
            $results = $model->execute($sql);
            //名字与价格拼接成串
			$cart_model = Controller::createMode('db_sys_26_71','t_26_user_product_cart_list'); 
            foreach($results as $key=>$val){	
				$pro_goods = $promtion_model->getOne("m_deliver_limit_time > '".date("Y-m-d H:i:s")."'  AND state=1 and item_no=".$val['item_no']);					
				$goods_stock = $model->execute("select top 1 * from t_im_branch_stock where item_no = '".$val['item_no']."'");
				$results[$key]['stock_qty'] = round($goods_stock[0]['stock_qty'],2);			
				$results[$key]['item_name'] = iconv("gbk","utf-8",$val['item_name']);
				$results[$key]['item_no'] = $val['item_no'];
				$results[$key]['unit_no'] = iconv('GBK', 'UTF-8', $val['unit_no']);
                $results[$key]['m_name'] = iconv("gbk","utf-8",$val['item_name']);
                $result[$key]['m_usual_price'] = '';
                $result[$key]['m_group_price'] = '';
				$cartinfo=$cart_model->getOne(array('m_id'=>$val['item_no'],'user_id'=>$this->jsonObj->user_id));
				$results[$key]['cart_total_nums'] = floatval($cartinfo['total']);
				$results[$key]['pro_code2'] = getProductRefundTitle($val);				
				if( $pro_goods['price']>0){
					$results[$key]['scheme_price'] = $pro_goods['price'];
				}
            }                 
			$this->ajax(200,null,array('list'=>$results,'data_total'=>$data_total));	          
    }
	
	public function test_accountAction(){
		$this->ajax(201,null,array('name'=>'0161','pwd'=>'666666'));		
	}

}